<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Groups/classes/PhpFox_ComponentGroupForumPosts.class.php, $Author:  $ */



/** Component for Group Forum Posts
 * @version $Id: PhpFox_ComponentGroupForumPosts.class.php
 *  Max Kovrigovich
 * @package module.groups
 */
class PhpFox_Mod_Groups_ComponentGroupForumPosts extends BaseComponent
{
    /** Processing method
     */

    function process()
    {
        //check permissions
        $oSecurityService = App::getModuleService('Account', 'Security');
		/* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */

        $oGroupsService = $this->_oModule->getService('Groups');
        /* @var $oGroupsService PhpFox_Mod_Groups_ServiceGroups */

        $oForumService = $this->_oModule->getService('GroupForum');
        /* @var $oForumService PhpFox_Mod_Groups_ServiceForum */

         $oSrvAcc = App::getModuleService('Account', 'Account');
	    /* @var $oSrvAcc PhpFox_Mod_Account_ServiceAccount */

        $oReq = &App::getRequest();
        /* @var $oReq Request */

        $oUrl = &App::getUrl();
        /* @var $oUrl Url */

        $oTpl = $this->_oModule->getTemplate();

        $oUser = $this->getParam('user');

        $sAction = $oReq->getAction();
        $sPost = '';
        $sSubject = '';
        $iPostId = $oReq->getInt('post', 0);
        $iTopicId = $oReq->getInt('id', 0);
        $aTopic = $oForumService->getPost($iTopicId);
        if($oReq->get('pg', '') == 'last')
            $iPage = 999999;
        else
            $iPage = $oReq->getInt('page', 1);

        $iQuoteId = $oReq->getInt('quote', 0);

        if(count($aTopic) == 0)
        {
            App::gotoUrl('public.groups');
        }

        $aGroupData = $oGroupsService->getGroupData($aTopic['group_id']);

        if ( $aGroupData['pub_forum'] == 'No' && !( (App::getUser('user') AND $oGroupsService->isMember($aTopic['group_id'], App::getUser('user'))) OR App::isStaff() ) )
        {
        	App::gotoUrl('public.groups');
        }

        if(count($aGroupData) == 0)
            App::gotoUrl('public.groups');

        $iGroupId = $aGroupData['id'];

        if(($sAction == 'post' || $sAction == 'edit' || $sAction == 'topic') && (!App::getUser('user')))
        {
            App::gotoUrl('public.login');
        }

        $bIsMember = $oGroupsService->isMember($iGroupId, App::getUser('user'));

        //check permissions
        if (($sAction == 'post' || $sAction == 'edit' || $sAction == 'topic') && !($bIsMember OR App::isStaff()) ) 
        {
        	App::gotoUrl('public.groups.private', array('id'=>$iGroupId, 'forum' => 'yes'));
        }

        if($sAction == 'post' && $iQuoteId != 0)
        {
            $aQuote = $oForumService->getPost($oReq->getInt('quote'));
            $sPost = '[quote="'.$aQuote['user'].'"]'.$aQuote['text'].'[/quote]';
        }
        if($sAction == 'edit')
        {        
            $aPost = $oForumService->getPost($iPostId);
            if($iPostId == $aPost['reply_id'])
                $sAction = 'topic';
            $sPost = $aPost['text'];
            $sSubject = $aPost['title'];
        }
        App::loadClass('Pager');
        $iPageSize = App::getParam('public.pagesize');

        list($aPosts, $iCnt) = $oForumService->getTopicPosts(array('reply_id'=>'= '.$iTopicId), $iPage, $iPageSize, 'gf.time ASC', $oUser);

        //submit form
        $sText = $oReq->get('text', '');

        if($sText != '')
        {
        	$oTxt = App::newObject('Text');
            $sText = trim($sText);
            $sText = $oTxt->preParseText($sText);
            if(strlen($sText))
            {
                $aParams = array();
                if(!$iPostId)
                {
                    $aParams = array('user'=>$oUser->aData['user'],
                                     'title'=>$oReq->get('title',''),
                                     'text'=>$sText,
                                     'time'=>time(),
                                     'type'=>'reply',
                                     'group_id'=>$aGroupData['id'],
                                     'reply_id'=>$iTopicId,);
                    $oPager = &new Pager($iCnt+1, $iPage, $iPageSize);
                    $iPage = $oPager->nPagesCount;
                }
                else
                {
                    $aParams = array('title'=>$oReq->get('title',''),
                                     'text'=>$oReq->get('text',''),);
                }

                $iNewPostId = $oForumService->editPost($aParams, $iTopicId, $iPostId);
                $aParams = array('id'=>$iTopicId, 'page'=>$iPage);
                if($iNewPostId)
                    $aParams['#'] = 'r'.$iNewPostId;
                else
                    $aParams['#'] = 'r'.$iPostId;

                App::gotoUrl('public.groups.forum.posts', $aParams);
            }

        }

        foreach($aPosts as $k=>$v)
	    {
            $aPosts[$k]['memb_image'] = $oSrvAcc->getMembershipImageByType($v['user_type']);
            $aPosts[$k]['stars_info'] = $oSrvAcc->getStarsAsArray($v['user_points']);
	    }
        $oPager = &new Pager($iCnt, $iPage, $iPageSize);
        $iFrom = $oPager->getOffset($iPage, $iPageSize);
        $iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);
	    $iPage = $oPager->nPage;

        if ($oUser)
        {
            $oForumService->markTopicView($iTopicId);
        }

        $oTpl->assignSrc(array(
            'sSubject'      => $sSubject,
            'sText'         => $sPost,
            'sAction'       => $sAction,
            'iFrom'         => ($iCnt) ? ($iFrom + 1) : 0,
            'iTo'           => $iTo,
            'aPaging'       => $oPager->getInfo($oUrl),
            'iCnt'          => $iCnt,
            'aPosts'        => $aPosts,
            'aTopic'        => $aTopic,
            'aGroupData'    => $aGroupData,
	        'sImageUrl'     => App::getParam('user.pic.url'),
            'iDate'         => time(),
	        'sMonthDateFormat' => App::getParam('format.monthdate%'),
            'aFormHidden'   => (($sAction == 'post')?array('pg'=>'last'):''),
	        ));

        $sHtml = $oTpl->fetch('GroupForumPosts.html');
        return $sHtml;
    }
}
?>